using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace UIDesigner
{
  class FormSrcMaker
  {
    public static string OutCreateWindow(Form form)
    {
      string str = "";
      str += "  hWnd = CreateWindowExA(\r\n";
      str += "           " + ExStyleString(form) + ",\r\n";
      str += "           " + Converter.Const("APP_NAME") + ",            //クラス名\r\n";
      str += "           " + Converter.Const("APP_NAME") + ",            //タイトルバーテキスト\r\n";
      str += "           " + StyleString(form) + ", //ウインドウスタイル\r\n";
      str += "           CW_USEDEFAULT,       //ウインドウ左上X座標\r\n";
      str += "           CW_USEDEFAULT,       //ウインドウ左上Y座標\r\n";
      str += "           " + form.Width + ", //WINDOW_WIDTH,        //ウインドウ幅\r\n";
      str += "           " + form.Height + ", //WINDOW_HEIGHT,       //ウインドウ高さ\r\n";
      str += "           NULL,                //親ウインドウのハンドル\r\n";
      str += "           NULL,\r\n";
      str += "           (HINSTANCE)GetModuleHandle(NULL),\r\n";
      str += "           NULL);\r\n";
      return str;
    }

    public static string OutWinMain(Form form)
    {
      Color c = form.BackColor;
      string str = "";
      str += @"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int nCmdShow)
{
  const char* " + Converter.Const("APP_NAME") + @" = """ + form.Name + @""";
  hInst = hInstance;
  WNDCLASSA wc;
  HWND hWnd;

  HBRUSH brush = CreateSolidBrush( RGB(" + c.R + ", " + c.G + ", " + c.B + @") );

  wc.style = CS_CLASSDC;
  wc.lpfnWndProc = WinProc;
  wc.cbClsExtra = wc.cbWndExtra =0;
  wc.hInstance = hInst;
  wc.hIcon = 0;
  wc.hCursor = 0;
  wc.hbrBackground = brush;
  wc.lpszMenuName = NULL;
  wc.lpszClassName = " + Converter.Const("APP_NAME") + @";
  if (RegisterClassA(&wc) == 0) {
    MessageBoxA(NULL, ""RegisterClass関数が失敗しました"", " + Converter.Const("APP_NAME") + @", NULL);
    return 0;
  }
"
        + OutCreateWindow(form) +
@"  if (!hWnd) {
    MessageBoxA(NULL, ""CreateWindow関数が失敗しました"", " + Converter.Const("APP_NAME") + @", NULL);
    return 0;
  }

  ShowWindow(hWnd, SW_SHOW);
  UpdateWindow(hWnd);

  MSG msg;

  while (GetMessage(&msg, NULL, 0, 0)) {
    if (!IsDialogMessage(hWnd, &msg))
    {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
  }
  DeleteObject(brush);
  FreeFont();
  return 0;
}";
      return str;
    }

    //スタイル文字列
    private static string StyleString(Form form)
    {
      string str = "";
      if (form.FormBorderStyle == FormBorderStyle.None)
      {
        str += "WS_POPUP"; // フレーム無し
      }
      else
      {
        str += "WS_SYSMENU";
        if (form.MaximizeBox)
        {
          str += "|WS_MAXIMIZEBOX";
        }
        if (form.MinimizeBox)
        {
          str += "|WS_MINIMIZEBOX";
        }
      }
      switch (form.FormBorderStyle)
      {
        case FormBorderStyle.FixedDialog:
          str += "|WS_DLGFRAME";
          break;
        case FormBorderStyle.FixedSingle:
          str += "|WS_DLGFRAME";
          break;
        case FormBorderStyle.Sizable:
          str += "|WS_SIZEBOX"; // WS_SIZEBOX は WS_THICKFRAME と同値
          break;
        case FormBorderStyle.SizableToolWindow:
          str += "|WS_SIZEBOX";
          break;
      }
      switch (form.WindowState)
      {
        case FormWindowState.Normal:
          str += "|SW_SHOW";
          break;
        case FormWindowState.Maximized:
          str += "|SW_MAXIMIZE";
          break;
        case FormWindowState.Minimized:
          str += "|SW_MINIMIZE";
          break;
      }
      return str;
    }
    //拡張スタイル文字列
    private static string ExStyleString(Form form)
    {
      string str = "0";
      switch (form.FormBorderStyle)
      {
        case FormBorderStyle.FixedToolWindow:
          str += "|WS_EX_TOOLWINDOW";
          break;
        case FormBorderStyle.SizableToolWindow:
          str += "|WS_EX_TOOLWINDOW";
          break;
      }
      if (form.TopMost)
      {
        str += "|WS_EX_TOPMOST";
      }
      if (form.AllowDrop)
      {
        str += "|WS_EX_ACCEPTFILES";
      }
      return str;
    }
  }
}
